<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- 下拉框 -->
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'

export default {
// 接收父组件传递的id
  props: {
    id: {
      type: String,
      required: true
    },
    //  用来判断是编辑还是添加
    isEdit: {
      type: Boolean,
      required: true
    }
  },

  data() {
    return {
      rules: { // 校验规则
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [] // 员工简单列表
    }
  },
  created() {
    this.getEmployees()
    this.loadDetail()
  },
  methods: {
    // 确定
    hSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        // 判断isEdit是true编辑还是false添加
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消
    hCancel() {
      this.$emit('close')
    },

    // 获取员工简单列表
    async getEmployees() {
      try {
        const res = await getEmployeeSimple()
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.employees = res.data
      } catch (error) {
        console.log(error)
      }
    },

    // 新增部门
    async doAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res)
        // 新增完成后通知父组件关闭弹窗
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },

    // 编辑
    async doEdit() {
      try {
        const res = await updateDepartments(this.form)
        console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },

    // 回现功能
    async loadDetail() {
      try {
        if (this.isEdit) {
          const res = await getDepartDetail(this.id)
          console.log(res)
          this.form = res.data
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
